﻿using System;
using System.Diagnostics;

namespace AutoIE
{
    static class Program
    {
        static bool bCompileOnly = false;
        static bool bRecord = false;
        static string scriptFile = "";
        static string capUrl = "";
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static int Main(string[] args)
        {
            BasicProcessor bp = null;
            int returnCode = 0;
            try
            {
                ParseArguments(args);
                if (bRecord)
                {
                    bp = new BasicProcessor();
                    Console.WriteLine("Start to record from : " + capUrl);
                    bp.StartRecord(capUrl);
                }
                else
                {
                    ProcessorCompiler pc = new ProcessorCompiler();
                    Generator gen = new Generator();
                    string code = gen.GenerateFromFile(scriptFile);
                    Console.WriteLine("Compiling...");
                    if (pc.Compile(code))
                    {
                        Console.WriteLine("Compiled successfully.");
                        if (bCompileOnly)
                            return 0;

                        bp = pc.CreateInstance(gen.FullClassName);//new PluProcessor(); //new SimpleProcessor(); //
                        if (bp == null)
                            throw new NullReferenceException("create object failed.");
                        Console.WriteLine("Start script...");
                        bp.Run();
                        Console.WriteLine("Script finished.");
                    }
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
                Debug.WriteLine(ex.ToString());
                returnCode = -1;
            }
            finally
            {
                if (bp != null)
                    bp.Dispose();
            }

            DebugReadKey();
            return returnCode;
        }

        [Conditional("DEBUG")]
        static void DebugReadKey()
        {
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }

        [Conditional("DEBUG")]
        static void SetDebugLog()
        {
            TextWriterTraceListener log = new TextWriterTraceListener("autoie.log");
            Debug.AutoFlush = true;
            Debug.Listeners.Add(log);
        }
        static void ParseArguments(string[] args)
        {
            bCompileOnly = false;
            bRecord = false;
            scriptFile = "";
            capUrl = "";
            if (args == null || args.Length < 1)
                throw new ArgumentException(@"Error: not enough arguments. 
Usage:
    AutoIE scriptfile.cs [/c] [/l] 
    AutoIE url /r

        /c compile only, do not execute script file
        /l enable log, in log.txt
        /r record actions in page");
            scriptFile = args[0];
            for (int i = 1; i < args.Length; i++)
            {
                switch (args[i].ToLower())
                {
                    case "/c":
                        bCompileOnly = true;
                        break;
                    case "/l":
                        SetDebugLog();
                        break;
                    case "/r":
                        bRecord = true;
                        scriptFile = "";
                        capUrl = args[0];
                        SetDebugLog();
                        break;
                }
            }
        }
    }
}
